<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.9.1"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Eigen: Block operations</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
  $(document).ready(function() { init_search(); });
/* @license-end */
</script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdn.mathjax.org/mathjax/latest/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="eigendoxy.css" rel="stylesheet" type="text/css">
<!--  -->
<script type="text/javascript" src="eigen_navtree_hacks.js"></script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="Eigen_Silly_Professor_64x64.png"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname"><a href="http://eigen.tuxfamily.org">Eigen</a>
   &#160;<span id="projectnumber">3.4.90 (git rev 67eeba6e720c5745abc77ae6c92ce0a44aa7b7ae)</span>
   </div>
  </td>
   <td>        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.svg"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.svg" alt=""/></a>
          </span>
        </div>
</td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.1 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
var searchBox = new SearchBox("searchBox", "search",false,'Search','.html');
/* @license-end */
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:cf05388f2679ee054f2beb29a391d25f4e673ac3&amp;dn=gpl-2.0.txt GPL-v2 */
$(document).ready(function(){initNavTree('group__TutorialBlockOperations.html',''); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Block operations<div class="ingroups"><a class="el" href="group__DenseMatrixManipulation__chapter.html">Dense matrix and array manipulation</a></div></div>  </div>
</div><!--header-->
<div class="contents">
<p>This page explains the essentials of block operations. A block is a rectangular part of a matrix or array. Blocks expressions can be used both as rvalues and as lvalues. As usual with <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> expressions, this abstraction has zero runtime cost provided that you let your compiler optimize.</p>
<h1><a class="anchor" id="TutorialBlockOperationsUsing"></a>
Using block operations</h1>
<p>The most general block operation in <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> is called <a class="el" href="">.block() </a>. There are two versions, whose syntax is as follows:</p>
<table class="manual">
<tr>
<th><b>Block</b> <b>operation</b>  </th><th>Version constructing a <br  />
 dynamic-size block expression </th><th>Version constructing a <br  />
 fixed-size block expression </th></tr>
<tr>
<td>Block of size <code>(p,q)</code>, starting at <code>(i,j)</code> </td><td><div class="fragment"><div class="line">matrix.block(i,j,p,q);</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">matrix.block&lt;p,q&gt;(i,j);</div>
</div><!-- fragment -->   </td></tr>
</table>
<p>As always in <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a>, indices start at 0.</p>
<p>Both versions can be used on fixed-size and dynamic-size matrices and arrays. These two expressions are semantically equivalent. The only difference is that the fixed-size version will typically give you faster code if the block size is small, but requires this size to be known at compile time.</p>
<p>The following program uses the dynamic-size and fixed-size versions to print the values of several blocks inside a matrix.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using namespace </span>std;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="classEigen_1_1Matrix.html">Eigen::MatrixXf</a> m(4,4);</div>
<div class="line">  m &lt;&lt;  1, 2, 3, 4,</div>
<div class="line">        5, 6, 7, 8,</div>
<div class="line">        9,10,11,12,</div>
<div class="line">       13,14,15,16;</div>
<div class="line">  cout &lt;&lt; <span class="stringliteral">&quot;Block in the middle&quot;</span> &lt;&lt; endl;</div>
<div class="line">  cout &lt;&lt; m.block&lt;2,2&gt;(1,1) &lt;&lt; endl &lt;&lt; endl;</div>
<div class="line">  <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 1; i &lt;= 3; ++i)</div>
<div class="line">  {</div>
<div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;Block of size &quot;</span> &lt;&lt; i &lt;&lt; <span class="stringliteral">&quot;x&quot;</span> &lt;&lt; i &lt;&lt; endl;</div>
<div class="line">    cout &lt;&lt; m.block(0,0,i,i) &lt;&lt; endl &lt;&lt; endl;</div>
<div class="line">  }</div>
<div class="line">}</div>
<div class="ttc" id="aclassEigen_1_1Matrix_html"><div class="ttname"><a href="classEigen_1_1Matrix.html">Eigen::Matrix</a></div><div class="ttdoc">The matrix class, also used for vectors and row-vectors.</div><div class="ttdef"><b>Definition:</b> Matrix.h:182</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">Block in the middle
 6  7
10 11

Block of size 1x1
1

Block of size 2x2
1 2
5 6

Block of size 3x3
 1  2  3
 5  6  7
 9 10 11

</pre> </td></tr>
</table>
<p>In the above example the <a class="el" href="">.block() </a> function was employed as a <em>rvalue</em>, i.e. it was only read from. However, blocks can also be used as <em>lvalues</em>, meaning that you can assign to a block.</p>
<p>This is illustrated in the following example. This example also demonstrates blocks in arrays, which works exactly like the above-demonstrated blocks in matrices.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="classEigen_1_1Array.html">Eigen::Array22f</a> m;</div>
<div class="line">  m &lt;&lt; 1,2,</div>
<div class="line">       3,4;</div>
<div class="line">  <a class="code" href="classEigen_1_1Array.html">Eigen::Array44f</a> a = <a class="code" href="classEigen_1_1DenseBase.html#a68a7ece6c5629d1e9447a321fcb14ccd">Eigen::Array44f::Constant</a>(0.6);</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Here is the array a:\n&quot;</span> &lt;&lt; a &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="line">  a.block&lt;2,2&gt;(1,1) = m;</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Here is now a with m copied into its central 2x2 block:\n&quot;</span> &lt;&lt; a &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="line">  a.block(0,0,2,3) = a.block(2,1,2,3);</div>
<div class="line">  std::cout &lt;&lt; <span class="stringliteral">&quot;Here is now a with bottom-right 2x3 block copied into top-left 2x3 block:\n&quot;</span> &lt;&lt; a &lt;&lt; <span class="stringliteral">&quot;\n\n&quot;</span>;</div>
<div class="line">}</div>
<div class="ttc" id="aclassEigen_1_1Array_html"><div class="ttname"><a href="classEigen_1_1Array.html">Eigen::Array</a></div><div class="ttdoc">General-purpose arrays with easy API for coefficient-wise operations.</div><div class="ttdef"><b>Definition:</b> Array.h:49</div></div>
<div class="ttc" id="aclassEigen_1_1DenseBase_html_a68a7ece6c5629d1e9447a321fcb14ccd"><div class="ttname"><a href="classEigen_1_1DenseBase.html#a68a7ece6c5629d1e9447a321fcb14ccd">Eigen::DenseBase::Constant</a></div><div class="ttdeci">static const ConstantReturnType Constant(Index rows, Index cols, const Scalar &amp;value)</div><div class="ttdef"><b>Definition:</b> CwiseNullaryOp.h:191</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">Here is the array a:
0.6 0.6 0.6 0.6
0.6 0.6 0.6 0.6
0.6 0.6 0.6 0.6
0.6 0.6 0.6 0.6

Here is now a with m copied into its central 2x2 block:
0.6 0.6 0.6 0.6
0.6   1   2 0.6
0.6   3   4 0.6
0.6 0.6 0.6 0.6

Here is now a with bottom-right 2x3 block copied into top-left 2x3 block:
  3   4 0.6 0.6
0.6 0.6 0.6 0.6
0.6   3   4 0.6
0.6 0.6 0.6 0.6

</pre> </td></tr>
</table>
<p>While the <a class="el" href="">.block() </a> method can be used for any block operation, there are other methods for special cases, providing more specialized API and/or better performance. On the topic of performance, all what matters is that you give <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> as much information as possible at compile time. For example, if your block is a single whole column in a matrix, using the specialized <a class="el" href="">.col() </a> function described below lets <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> know that, which can give it optimization opportunities.</p>
<p>The rest of this page describes these specialized methods.</p>
<h1><a class="anchor" id="TutorialBlockOperationsSyntaxColumnRows"></a>
Columns and rows</h1>
<p>Individual columns and rows are special cases of blocks. <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> provides methods to easily address them: <a class="el" href="">.col() </a> and <a class="el" href="">.row()</a>.</p>
<table class="manual">
<tr>
<th>Block operation </th><th>Method </th></tr>
<tr>
<td>i<sup>th</sup> row <a class="el" href="">* </a> </td><td><div class="fragment"><div class="line">matrix.row(i);</div>
</div><!-- fragment -->   </td></tr>
<tr>
<td>j<sup>th</sup> column <a class="el" href="">* </a> </td><td><div class="fragment"><div class="line">matrix.col(j);</div>
</div><!-- fragment -->   </td></tr>
</table>
<p>The argument for <code>col()</code> and <code>row()</code> is the index of the column or row to be accessed. As always in <a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a>, indices start at 0.</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using namespace </span>std;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="classEigen_1_1Matrix.html">Eigen::MatrixXf</a> m(3,3);</div>
<div class="line">  m &lt;&lt; 1,2,3,</div>
<div class="line">       4,5,6,</div>
<div class="line">       7,8,9;</div>
<div class="line">  cout &lt;&lt; <span class="stringliteral">&quot;Here is the matrix m:&quot;</span> &lt;&lt; endl &lt;&lt; m &lt;&lt; endl;</div>
<div class="line">  cout &lt;&lt; <span class="stringliteral">&quot;2nd Row: &quot;</span> &lt;&lt; m.row(1) &lt;&lt; endl;</div>
<div class="line">  m.col(2) += 3 * m.col(0);</div>
<div class="line">  cout &lt;&lt; <span class="stringliteral">&quot;After adding 3 times the first column into the third column, the matrix m is:\n&quot;</span>;</div>
<div class="line">  cout &lt;&lt; m &lt;&lt; endl;</div>
<div class="line">}</div>
</div><!-- fragment -->  </td><td><pre class="fragment">Here is the matrix m:
1 2 3
4 5 6
7 8 9
2nd Row: 4 5 6
After adding 3 times the first column into the third column, the matrix m is:
 1  2  6
 4  5 18
 7  8 30
</pre> </td></tr>
</table>
<p>That example also demonstrates that block expressions (here columns) can be used in arithmetic like any other expression.</p>
<h1><a class="anchor" id="TutorialBlockOperationsSyntaxCorners"></a>
Corner-related operations</h1>
<p><a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> also provides special methods for blocks that are flushed against one of the corners or sides of a matrix or array. For instance, <a class="el" href="">.topLeftCorner() </a> can be used to refer to a block in the top-left corner of a matrix.</p>
<p>The different possibilities are summarized in the following table:</p>
<table class="manual">
<tr>
<th>Block <b>operation</b>  </th><th>Version constructing a <br  />
 dynamic-size block expression </th><th>Version constructing a <br  />
 fixed-size block expression </th></tr>
<tr>
<td>Top-left p by q block <a class="el" href="">* </a> </td><td><div class="fragment"><div class="line">matrix.topLeftCorner(p,q);</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">matrix.topLeftCorner&lt;p,q&gt;();</div>
</div><!-- fragment -->   </td></tr>
<tr>
<td>Bottom-left p by q block <a class="el" href="">* </a> </td><td><div class="fragment"><div class="line">matrix.bottomLeftCorner(p,q);</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">matrix.bottomLeftCorner&lt;p,q&gt;();</div>
</div><!-- fragment -->   </td></tr>
<tr>
<td>Top-right p by q block <a class="el" href="">* </a> </td><td><div class="fragment"><div class="line">matrix.topRightCorner(p,q);</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">matrix.topRightCorner&lt;p,q&gt;();</div>
</div><!-- fragment -->   </td></tr>
<tr>
<td>Bottom-right p by q block <a class="el" href="">* </a> </td><td><div class="fragment"><div class="line">matrix.bottomRightCorner(p,q);</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">matrix.bottomRightCorner&lt;p,q&gt;();</div>
</div><!-- fragment -->   </td></tr>
<tr>
<td>Block containing the first q rows <a class="el" href="">* </a> </td><td><div class="fragment"><div class="line">matrix.topRows(q);</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">matrix.topRows&lt;q&gt;();</div>
</div><!-- fragment -->   </td></tr>
<tr>
<td>Block containing the last q rows <a class="el" href="">* </a> </td><td><div class="fragment"><div class="line">matrix.bottomRows(q);</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">matrix.bottomRows&lt;q&gt;();</div>
</div><!-- fragment -->   </td></tr>
<tr>
<td>Block containing the first p columns <a class="el" href="">* </a> </td><td><div class="fragment"><div class="line">matrix.leftCols(p);</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">matrix.leftCols&lt;p&gt;();</div>
</div><!-- fragment -->   </td></tr>
<tr>
<td>Block containing the last q columns <a class="el" href="">* </a> </td><td><div class="fragment"><div class="line">matrix.rightCols(q);</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">matrix.rightCols&lt;q&gt;();</div>
</div><!-- fragment -->   </td></tr>
<tr>
<td>Block containing the q columns starting from i <a class="el" href="">* </a> </td><td><div class="fragment"><div class="line">matrix.middleCols(i,q);</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">matrix.middleCols&lt;q&gt;(i);</div>
</div><!-- fragment -->   </td></tr>
<tr>
<td>Block containing the q rows starting from i <a class="el" href="">* </a> </td><td><div class="fragment"><div class="line">matrix.middleRows(i,q);</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">matrix.middleRows&lt;q&gt;(i);</div>
</div><!-- fragment -->   </td></tr>
</table>
<p>Here is a simple example illustrating the use of the operations presented above:</p>
<table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using namespace </span>std;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="classEigen_1_1Matrix.html">Eigen::Matrix4f</a> m;</div>
<div class="line">  m &lt;&lt; 1, 2, 3, 4,</div>
<div class="line">       5, 6, 7, 8,</div>
<div class="line">       9, 10,11,12,</div>
<div class="line">       13,14,15,16;</div>
<div class="line">  cout &lt;&lt; <span class="stringliteral">&quot;m.leftCols(2) =&quot;</span> &lt;&lt; endl &lt;&lt; m.leftCols(2) &lt;&lt; endl &lt;&lt; endl;</div>
<div class="line">  cout &lt;&lt; <span class="stringliteral">&quot;m.bottomRows&lt;2&gt;() =&quot;</span> &lt;&lt; endl &lt;&lt; m.bottomRows&lt;2&gt;() &lt;&lt; endl &lt;&lt; endl;</div>
<div class="line">  m.topLeftCorner(1,3) = m.bottomRightCorner(3,1).<a class="code" href="classEigen_1_1DenseBase.html#a43cbcd866a0737eb56642c2e992f0afd">transpose</a>();</div>
<div class="line">  cout &lt;&lt; <span class="stringliteral">&quot;After assignment, m = &quot;</span> &lt;&lt; endl &lt;&lt; m &lt;&lt; endl;</div>
<div class="line">}</div>
<div class="ttc" id="aclassEigen_1_1DenseBase_html_a43cbcd866a0737eb56642c2e992f0afd"><div class="ttname"><a href="classEigen_1_1DenseBase.html#a43cbcd866a0737eb56642c2e992f0afd">Eigen::DenseBase::transpose</a></div><div class="ttdeci">TransposeReturnType transpose()</div><div class="ttdef"><b>Definition:</b> Transpose.h:184</div></div>
</div><!-- fragment -->  </td><td><pre class="fragment">m.leftCols(2) =
 1  2
 5  6
 9 10
13 14

m.bottomRows&lt;2&gt;() =
 9 10 11 12
13 14 15 16

After assignment, m = 
 8 12 16  4
 5  6  7  8
 9 10 11 12
13 14 15 16
</pre> </td></tr>
</table>
<h1><a class="anchor" id="TutorialBlockOperationsSyntaxVectors"></a>
Block operations for vectors</h1>
<p><a class="el" href="namespaceEigen.html" title="Namespace containing all symbols from the Eigen library.">Eigen</a> also provides a set of block operations designed specifically for the special case of vectors and one-dimensional arrays:</p>
<table class="manual">
<tr>
<th>Block operation </th><th>Version constructing a <br  />
 dynamic-size block expression </th><th>Version constructing a <br  />
 fixed-size block expression </th></tr>
<tr>
<td>Block containing the first <code>n</code> elements <a class="el" href="">* </a> </td><td><div class="fragment"><div class="line">vector.head(n);</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">vector.head&lt;n&gt;();</div>
</div><!-- fragment -->   </td></tr>
<tr>
<td>Block containing the last <code>n</code> elements <a class="el" href="">* </a> </td><td><div class="fragment"><div class="line">vector.tail(n);</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">vector.tail&lt;n&gt;();</div>
</div><!-- fragment -->   </td></tr>
<tr>
<td>Block containing <code>n</code> elements, starting at position <code>i</code> <a class="el" href="">* </a> </td><td><div class="fragment"><div class="line">vector.segment(i,n);</div>
</div><!-- fragment -->  </td><td><div class="fragment"><div class="line">vector.segment&lt;n&gt;(i);</div>
</div><!-- fragment -->   </td></tr>
</table>
<p>An example is presented below: </p><table class="example">
<tr>
<th>Example:</th><th>Output: </th></tr>
<tr>
<td><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;Eigen/Dense&gt;</span></div>
<div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div>
<div class="line"> </div>
<div class="line"><span class="keyword">using namespace </span>std;</div>
<div class="line"> </div>
<div class="line"><span class="keywordtype">int</span> main()</div>
<div class="line">{</div>
<div class="line">  <a class="code" href="classEigen_1_1Array.html">Eigen::ArrayXf</a> v(6);</div>
<div class="line">  v &lt;&lt; 1, 2, 3, 4, 5, 6;</div>
<div class="line">  cout &lt;&lt; <span class="stringliteral">&quot;v.head(3) =&quot;</span> &lt;&lt; endl &lt;&lt; v.head(3) &lt;&lt; endl &lt;&lt; endl;</div>
<div class="line">  cout &lt;&lt; <span class="stringliteral">&quot;v.tail&lt;3&gt;() = &quot;</span> &lt;&lt; endl &lt;&lt; v.tail&lt;3&gt;() &lt;&lt; endl &lt;&lt; endl;</div>
<div class="line">  v.segment(1,4) *= 2;</div>
<div class="line">  cout &lt;&lt; <span class="stringliteral">&quot;after &#39;v.segment(1,4) *= 2&#39;, v =&quot;</span> &lt;&lt; endl &lt;&lt; v &lt;&lt; endl;</div>
<div class="line">}</div>
</div><!-- fragment -->  </td><td><pre class="fragment">v.head(3) =
1
2
3

v.tail&lt;3&gt;() = 
4
5
6

after 'v.segment(1,4) *= 2', v =
 1
 4
 6
 8
10
 6
</pre> </td></tr>
</table>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
  <ul>
    <li class="footer">Generated on Thu Apr 21 2022 13:07:55 for Eigen by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.9.1 </li>
  </ul>
</div>
</body>
</html>
